package org.example.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * @author xuan
 * @create 2023/9/19
 */
public class ClientHandler extends ChannelInboundHandlerAdapter {

    /**
     * @Description 当通道就绪时会触发该方法
     * @date 2020/7/23 11:27
     * @param ctx
     * @return void
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        StudentPOJO.Student student = StudentPOJO.Student.newBuilder()
                .setId(4)
                .setName("重庆刘德华,香港徐大虾")
                .build();

        ctx.writeAndFlush(student);
    }

    /**
     * @Description 当通道有读取事件时，会触发
     * @date 2020/7/23 11:29
     * @param ctx
     * @param msg
     * @return void
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf=(ByteBuf)msg;
        System.out.println("服务器回复的消息："+((ByteBuf) msg).toString(CharsetUtil.UTF_8));
        System.out.println("服务器的地址："+ctx.channel().remoteAddress());
    }
}

